package net.synck.gcontact.test;

import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.layout.GridData;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.jface.viewers.IContentProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;

import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.TextConstruct;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.extensions.PhoneNumber;

public class ContactEditorComposite2 extends Composite {

	private ContactEntry input = null;
		
	private FormToolkit formToolkit = null;   //  @jve:decl-index=0:visual-constraint=""
	private Composite compositeMain = null;
	//private Form formMain = null;
	private Composite formMainComposite = null;
	private Section sectionEmails = null;
	private Composite compositeEmails = null;
	private Composite compositeEmailList = null;
	private Tree tree = null;
	private Button buttonAddEmail = null;
	private Button buttonRemoveEmail = null;
	private Composite compositeEmailDetails = null;
	private Label labelAdressEmail = null;
	private Label labelEmailType = null;
	private Label labelEmailPrimary = null;
	private Text textEmailAdress = null;
	private Combo comboEmailType = null;
	private Button checkBoxEmailPrimary = null;
	private Section sectionPhones = null;
	private SashForm sashForm = null;
	private Composite compositePhoneList = null;
	private Composite compositePhoneDetails = null;
	private Tree tree1PhoneList = null;
	private Button buttonAddPhone = null;
	private Button buttonPhoneRemove = null;
	private Label labelPhone = null;
	private Label labelPhoneType = null;
	private Text textPhoneBumber = null;
	private Combo comboPhoneType = null;
	private Button checkBoxPhonePrimary = null;
	private Section sectionGeneral = null;
	private Composite compositeGeneral = null;
	private Text textContactName = null;
	private TreeViewer treeViewer = null;	
	public ContactEditorComposite2(Composite parent, int style) {
		super(parent, style);
		initialize();
	}

	private void initialize() {
		createCompositeMain();
		setSize(new Point(568, 388));
		setLayout(new GridLayout());
		setupTreeViewers();
	}

	private void setupTreeViewers() {
		treeViewer.setContentProvider(new ITreeContentProvider() {

			@Override
			public Object[] getChildren(Object parentElement) {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public Object getParent(Object element) {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public boolean hasChildren(Object element) {
				// TODO Auto-generated method stub
				return false;
			}

			@Override
			public Object[] getElements(Object inputElement) {
				ContactEntry contact = (ContactEntry)inputElement;
				return contact.getPhoneNumbers().toArray();
			}

			@Override
			public void dispose() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void inputChanged(Viewer viewer, Object oldInput,
					Object newInput) {
				// TODO Auto-generated method stub				
			}
					
		});
		
		treeViewer.setLabelProvider(new ILabelProvider() {

			@Override
			public Image getImage(Object element) {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public String getText(Object element) {
				if (element instanceof PhoneNumber ) {
					PhoneNumber number = (PhoneNumber)element;
					return (number.getPhoneNumber() + "(" + number.getLabel() +")");
				}
				return null;
			}

			@Override
			public void addListener(ILabelProviderListener listener) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void dispose() {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public boolean isLabelProperty(Object element, String property) {
				// TODO Auto-generated method stub
				return false;
			}

			@Override
			public void removeListener(ILabelProviderListener listener) {
				// TODO Auto-generated method stub
				
			}
			
		});
	}
	
	/**
	 * This method initializes formToolkit	
	 * 	
	 * @return org.eclipse.ui.forms.widgets.FormToolkit	
	 */
	private FormToolkit getFormToolkit() {
		if (formToolkit == null) {
			formToolkit = new FormToolkit(Display.getCurrent());
		}
		return formToolkit;
	}

	/**
	 * This method initializes compositeMain	
	 *
	 */
	private void createCompositeMain() {
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.verticalAlignment = GridData.FILL;
		compositeMain = getFormToolkit().createComposite(this);
		compositeMain.setLayout(new GridLayout());
		createFormMain();
		compositeMain.setLayoutData(gridData);
	}

	/**
	 * This method initializes formMain	
	 *
	 */
	private void createFormMain() {
		GridData gridData1 = new GridData();
		gridData1.grabExcessHorizontalSpace = true;
		gridData1.horizontalAlignment = GridData.FILL;
		gridData1.verticalAlignment = GridData.FILL;
		gridData1.grabExcessVerticalSpace = true;
		
		formMainComposite = getFormToolkit().createComposite(compositeMain);
		formMainComposite.setLayoutData(gridData1);
//		formMain = getFormToolkit().createForm(compositeMain);
		createSectionGeneral();
//		formMain.setText("Main");
//		formMain.setLayoutData(gridData1);
		
		createSectionEmails();
		
		formMainComposite.setLayout(new GridLayout());
//		formMain.getBody().setLayout(new GridLayout());
		createSectionPhones();
		
	}

	/**
	 * This method initializes sectionEmails	
	 *
	 */
	private void createSectionEmails() {
		GridData gridData2 = new GridData();
		gridData2.horizontalAlignment = GridData.FILL;
		gridData2.grabExcessHorizontalSpace = true;
		gridData2.verticalAlignment = GridData.CENTER;
		sectionEmails = getFormToolkit().createSection(
				formMainComposite,
				//formMain.getBody(),
				ExpandableComposite.TWISTIE | Section.DESCRIPTION
						| ExpandableComposite.TITLE_BAR);
		sectionEmails.setExpanded(true);
		sectionEmails.setDescription("Email adresses");
		createCompositeEmails();
		sectionEmails.setText("Emails");
		sectionEmails.setLayoutData(gridData2);
		sectionEmails.setClient(compositeEmails);
	}

	/**
	 * This method initializes compositeEmails	
	 *
	 */
	private void createCompositeEmails() {
		GridData gridData6 = new GridData();
		gridData6.horizontalAlignment = GridData.BEGINNING;
		gridData6.widthHint = 3;
		gridData6.verticalAlignment = GridData.FILL;
		GridLayout gridLayout1 = new GridLayout();
		gridLayout1.numColumns = 3;
		compositeEmails = getFormToolkit().createComposite(sectionEmails);
		compositeEmails.setLayout(gridLayout1);
		createCompositeEmailList();
		Composite separator = getFormToolkit().createCompositeSeparator(compositeEmails);
		separator.setLayoutData(gridData6);
		createCompositeEmailDetails();
	}

	/**
	 * This method initializes compositeEmailList	
	 *
	 */
	private void createCompositeEmailList() {
		GridData gridData5 = new GridData();
		gridData5.horizontalAlignment = GridData.FILL;
		gridData5.verticalAlignment = GridData.BEGINNING;
		GridData gridData4 = new GridData();
		gridData4.horizontalAlignment = GridData.FILL;
		gridData4.verticalAlignment = GridData.BEGINNING;
		GridData gridData3 = new GridData();
		gridData3.verticalSpan = 2;
		gridData3.widthHint = 150;
		GridLayout gridLayout2 = new GridLayout();
		gridLayout2.numColumns = 2;
		compositeEmailList = getFormToolkit().createComposite(compositeEmails);
		compositeEmailList.setLayout(gridLayout2);
		tree = getFormToolkit().createTree(compositeEmailList, SWT.BORDER);
		tree.setLayoutData(gridData3);
		buttonAddEmail = getFormToolkit().createButton(compositeEmailList, "Add", SWT.PUSH);
		buttonAddEmail.setLayoutData(gridData4);
		buttonRemoveEmail = getFormToolkit().createButton(compositeEmailList, "Remove", SWT.PUSH);
		buttonRemoveEmail.setLayoutData(gridData5);
	}

	/**
	 * This method initializes compositeEmailDetails	
	 *
	 */
	private void createCompositeEmailDetails() {
		GridData gridData8 = new GridData();
		gridData8.horizontalAlignment = GridData.FILL;
		gridData8.grabExcessHorizontalSpace = true;
		gridData8.verticalAlignment = GridData.CENTER;
		GridLayout gridLayout3 = new GridLayout();
		gridLayout3.numColumns = 2;
		GridData gridData7 = new GridData();
		gridData7.horizontalAlignment = GridData.FILL;
		gridData7.grabExcessHorizontalSpace = true;
		gridData7.verticalAlignment = GridData.FILL;
		compositeEmailDetails = getFormToolkit().createComposite(compositeEmails);
		compositeEmailDetails.setLayoutData(gridData7);
		compositeEmailDetails.setLayout(gridLayout3);
		labelAdressEmail = getFormToolkit().createLabel(compositeEmailDetails, "Adress:");
		textEmailAdress = getFormToolkit().createText(compositeEmailDetails, null, SWT.SINGLE | SWT.BORDER);
		textEmailAdress.setLayoutData(gridData8);
		labelEmailType = getFormToolkit().createLabel(compositeEmailDetails, "Type:");
		createComboEmailType();
		labelEmailPrimary = getFormToolkit().createLabel(compositeEmailDetails, "Primary:");
		checkBoxEmailPrimary = getFormToolkit().createButton(compositeEmailDetails, null, SWT.CHECK);
	}
	/**
	 * This method initializes comboEmailType	
	 *
	 */
	private void createComboEmailType() {
		GridData gridData9 = new GridData();
		gridData9.horizontalAlignment = GridData.FILL;
		gridData9.verticalAlignment = GridData.CENTER;
		comboEmailType = new Combo(compositeEmailDetails, SWT.NONE);
		comboEmailType.setLayoutData(gridData9);
	}

	/**
	 * This method initializes sectionPhones	
	 *
	 */
	private void createSectionPhones() {
		GridData gridData11 = new GridData();
		gridData11.horizontalAlignment = GridData.FILL;
		gridData11.grabExcessHorizontalSpace = true;
		gridData11.verticalAlignment = GridData.CENTER;
		sectionPhones = getFormToolkit().createSection(
				formMainComposite,
				ExpandableComposite.TWISTIE | Section.DESCRIPTION
						| ExpandableComposite.TITLE_BAR);
		sectionPhones.setExpanded(true);
		sectionPhones.setDescription("Phone numbers");
		sectionPhones.setLayoutData(gridData11);
		createSashForm();
		sectionPhones.setText("Phones");
		sectionPhones.setClient(sashForm);
	}

	/**
	 * This method initializes sashForm	
	 *
	 */
	private void createSashForm() {
		sashForm = new SashForm(sectionPhones, SWT.NONE);
		sashForm.setOrientation(SWT.HORIZONTAL);
		createCompositePhoneList();
		createCompositePhoneDetails();
	}

	/**
	 * This method initializes compositePhoneList	
	 *
	 */
	private void createCompositePhoneList() {
		GridData gridData14 = new GridData();
		gridData14.horizontalAlignment = GridData.FILL;
		gridData14.verticalAlignment = GridData.BEGINNING;
		GridData gridData13 = new GridData();
		gridData13.grabExcessHorizontalSpace = false;
		gridData13.verticalAlignment = GridData.BEGINNING;
		gridData13.horizontalAlignment = GridData.FILL;
		GridData gridData12 = new GridData();
		gridData12.verticalSpan = 2;
		gridData12.verticalAlignment = GridData.CENTER;
		gridData12.grabExcessHorizontalSpace = true;
		gridData12.horizontalAlignment = GridData.FILL;
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		compositePhoneList = new Composite(sashForm, SWT.NONE);
		compositePhoneList.setLayout(gridLayout);
		tree1PhoneList = getFormToolkit().createTree(compositePhoneList, SWT.BORDER);
		tree1PhoneList.setLayoutData(gridData12);
		treeViewer = new TreeViewer(tree1PhoneList);
		buttonAddPhone = getFormToolkit().createButton(compositePhoneList, "Add", SWT.PUSH);
		buttonAddPhone.setLayoutData(gridData13);
		buttonPhoneRemove = getFormToolkit().createButton(compositePhoneList, "Remove", SWT.PUSH);
		buttonPhoneRemove.setLayoutData(gridData14);
	}

	/**
	 * This method initializes compositePhoneDetails	
	 *
	 */
	private void createCompositePhoneDetails() {
		GridData gridData16 = new GridData();
		gridData16.grabExcessHorizontalSpace = true;
		gridData16.verticalAlignment = GridData.CENTER;
		gridData16.horizontalAlignment = GridData.FILL;
		GridData gridData15 = new GridData();
		gridData15.horizontalAlignment = GridData.END;
		gridData15.verticalAlignment = GridData.CENTER;
		GridLayout gridLayout4 = new GridLayout();
		gridLayout4.numColumns = 2;
		compositePhoneDetails = new Composite(sashForm, SWT.NONE);
		compositePhoneDetails.setLayout(gridLayout4);
		labelPhone = getFormToolkit().createLabel(compositePhoneDetails, "Phone:");
		textPhoneBumber = getFormToolkit().createText(compositePhoneDetails, null, SWT.SINGLE | SWT.BORDER);
		textPhoneBumber.setLayoutData(gridData16);
		labelPhoneType = getFormToolkit().createLabel(compositePhoneDetails, "Type:");
		createComboPhoneType();
		Label filler = new Label(compositePhoneDetails, SWT.NONE);
		checkBoxPhonePrimary = getFormToolkit().createButton(compositePhoneDetails, "Primary", SWT.CHECK);
		checkBoxPhonePrimary.setLayoutData(gridData15);
	}

	/**
	 * This method initializes comboPhoneType	
	 *
	 */
	private void createComboPhoneType() {
		GridData gridData17 = new GridData();
		gridData17.horizontalAlignment = GridData.FILL;
		gridData17.verticalAlignment = GridData.CENTER;
		comboPhoneType = new Combo(compositePhoneDetails, SWT.NONE);
		comboPhoneType.setLayoutData(gridData17);
	}

	/**
	 * This method initializes sectionGeneral	
	 *
	 */
	private void createSectionGeneral() {
		GridData gridData10 = new GridData();
		gridData10.horizontalAlignment = GridData.FILL;
		gridData10.grabExcessHorizontalSpace = true;
		gridData10.verticalAlignment = GridData.CENTER;
		sectionGeneral = getFormToolkit().createSection(
				formMainComposite,
				ExpandableComposite.TWISTIE | Section.DESCRIPTION
						| ExpandableComposite.TITLE_BAR);
		sectionGeneral.setExpanded(true);
		sectionGeneral.setLayoutData(gridData10);
		createCompositeGeneral();
		sectionGeneral.setText("General");
		sectionGeneral.setClient(compositeGeneral);
	}

	/**
	 * This method initializes compositeGeneral	
	 *
	 */
	private void createCompositeGeneral() {
		GridData gridData18 = new GridData();
		gridData18.grabExcessHorizontalSpace = true;
		gridData18.verticalAlignment = GridData.CENTER;
		gridData18.horizontalAlignment = GridData.FILL;
		compositeGeneral = getFormToolkit().createComposite(sectionGeneral);
		compositeGeneral.setLayout(new GridLayout());
		textContactName = getFormToolkit().createText(compositeGeneral, null, SWT.SINGLE | SWT.BORDER);
		textContactName.setLayoutData(gridData18);
	}


	public void setInput(ContactEntry contact) {
		input = contact;
		textContactName.setText(contact.getTitle().getPlainText());
		treeViewer.setInput(input);
	}
	
	public ContactEntry getInput() {
		if (!input.getTitle().getPlainText().equals(textContactName.getText())) {
			input.setTitle(new PlainTextConstruct(textContactName.getText()));
		}
		return input;
	}

	
	/*
	public void setContactName(TextConstruct title) {
		textContactName.setText(title.getPlainText());
	}
	
	public TextConstruct getContactName() {
		return new PlainTextConstruct(textContactName.getText());
	}
	
	public void setPhones() {
				
	}
	*/
}  //  @jve:decl-index=0:visual-constraint="10,10"
